<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Models\WorkUser;
use App\Models\Base\SchoolUser;
use App\Admin\Repositories\School;
use App\Enums\WorkTicketStatusEnum;
use App\Admin\Repositories\WorkTicket;
use Dcat\Admin\Http\Controllers\AdminController;

/**
 * @mixin \App\Models\School
 */
class SchoolController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new School(['users','workUsers','tickets']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('users','学生数量')->display(function () {
                return $this->users->count();
            });
            $grid->column('workUsers','维修人员数量')->display(function () {
                return $this->workUsers->count();
            });
            $grid->column('tickets','工单数量')->display(function () {
                return $this->tickets->count();
            });
            $grid->column('address');
            $grid->column('tel');
            $grid->column('memo');
            $grid->column('created_at')->format('Y-m-d H:i:s')->sortable();
            $grid->column('updated_at')->format('Y-m-d H:i:s')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->width(2);
                $filter->like('name')->width(3);
                $filter->like('address')->width(3);
                $filter->like('tel')->width(3);
                $filter->like('memo')->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new School(['users', 'tickets','workUsers']), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('address');
            $show->field('tel');
            $show->field('memo');
            $show->field('created_at');
            $show->field('updated_at');

            $show->relation('users','学生列表', function ($model) {
                $grid = new Grid(new SchoolUser);

                $grid->model()->where('school_id', $model->id);

                // 设置路由
                $grid->setResource('school-users');

                $grid->id();
                $grid->column('real_name','姓名');
                $grid->column('account','账号');
                $grid->column('tel','电话');
                $grid->column('id_card','身份证号');
                $grid->column('enable','状态')->bool();
                $grid->column('memo','备注');
                $grid->column('created_at')->format('Y-m-d H:i:s')->sortable();
                $grid->column('updated_at')->format('Y-m-d H:i:s')->sortable();

                $grid->filter(function ($filter) {
                    $filter->panel();
                    $filter->like('real_name')->width(2);
                    $filter->like('account')->width(2);
                    $filter->like('tel')->width(2);
                    $filter->like('id_card')->width(2);
                    $filter->equal('enable')->select([1 => '可用', 0 => '禁用'])->width(2);
                    $filter->like('memo')->width(2);
                });

                return $grid;
            });

            $show->relation('workUsers','维修人员列表', function ($model) {
                $grid = new Grid(new WorkUser);
                $grid->model()->whereHas('schools', function ($query) use ($model) {
                    $query->where('school_id', $model->id);
                });
                $grid->setResource('work-users');
                    $grid->id();
                    $grid->column('real_name','姓名');
                    $grid->column('tel','电话');
                    $grid->column('enable','状态')->bool();
                    $grid->column('memo','备注');
                    $grid->column('created_at')->format('Y-m-d H:i:s')->sortable();
                    $grid->column('updated_at')->format('Y-m-d H:i:s')->sortable();

                $grid->filter(function ($filter) {
                    $filter->panel();
                    $filter->like('real_name','姓名')->width(2);
                    $filter->like('tel','电话')->width(2);
                    $filter->equal('enable','状态')->select([1 => '可用', 0 => '禁用'])->width(2);
                    $filter->like('memo','备注')->width(2);
                });
                return $grid;
            });

            $show->relation('tickets','工单列表', function ($model) {
                $grid = new Grid(new WorkTicket);
                $grid->model()->where('school_id', $model->id);
                $grid->setResource('work-tickets');
                $grid->id();
                $grid->column('code','工单号');
                $grid->column('schoolUser.real_name','学生姓名');
                $grid->column('room_number','房间号');
                $grid->column('contact_name','联系人');
                $grid->column('contact_tel','联系电话');
                $grid->column('content','内容');
                $grid->column('workUser.real_name','维修人员');
                $grid->column('status')->using(WorkTicketStatusEnum::asSelectArray());
                $grid->column('finish_at')->format('Y-m-d H:i:s')->sortable();
                $grid->column('created_at')->format('Y-m-d H:i:s')->sortable();
                $grid->column('updated_at')->format('Y-m-d H:i:s')->sortable();

                $grid->filter(function ($filter) {
                    $filter->panel();
                    $filter->like('code')->width(2);
                    $filter->like('schoolUser.real_name','学生姓名')->width(2);
                    $filter->like('workUser.real_name','维修人员')->width(2);
                    $filter->in('status')->multipleSelect(WorkTicketStatusEnum::asSelectArray())->width(4);
                    $filter->between('finish_at')->date()->width(4);
                    $filter->between('created_at')->date()->width(4);
                    $filter->between('updated_at')->date()->width(4);
                });
                return $grid;
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new School(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->text('address');
            $form->text('tel');
            $form->text('memo');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
